<template>
  <div class="app-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h2 class="page-title">售后管理</h2>
      <div class="page-subtitle">管理系统中的售后申请和状态</div>
    </div>

    <!-- 搜索区域 -->
    <div class="search-card">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
        <div class="search-row">
          <el-form-item label="售后ID" prop="afterSaleId">
            <el-input
                v-model="queryParams.afterSaleId"
                placeholder="请输入售后ID"
                clearable
                size="medium"
                @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="订单ID" prop="orderId">
            <el-input
                v-model="queryParams.orderId"
                placeholder="请输入订单ID"
                clearable
                size="medium"
                @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="用户ID" prop="userId">
            <el-input
                v-model="queryParams.userId"
                placeholder="请输入用户ID"
                clearable
                size="medium"
                @keyup.enter="handleQuery"
            />
          </el-form-item>
        </div>

        <div class="search-row">
          <el-form-item label="售后类型" prop="type">
            <el-select
                v-model="queryParams.type"
                placeholder="请选择售后类型"
                clearable
                size="medium"
                style="width: 180px"
            >
              <el-option
                  v-for="dict in mall_after_sale_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="申请金额" prop="amount">
            <el-input
                v-model="queryParams.amount"
                placeholder="请输入申请金额"
                clearable
                size="medium"
                style="width: 150px"
                @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="售后状态" prop="status">
            <el-select
                v-model="queryParams.status"
                placeholder="请选择状态"
                clearable
                size="medium"
                style="width: 180px"
            >
              <el-option
                  v-for="dict in mall_after_sale_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </div>

        <div class="search-row">
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker
                clearable
                v-model="queryParams.createTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="选择创建时间"
                size="medium"
                style="width: 180px"
            />
          </el-form-item>
          <el-form-item label="更新时间" prop="updateTime">
            <el-date-picker
                clearable
                v-model="queryParams.updateTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="选择更新时间"
                size="medium"
                style="width: 180px"
            />
          </el-form-item>
          <el-form-item class="search-actions">
            <el-button type="primary" icon="Search" @click="handleQuery" size="medium">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery" size="medium">重置</el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="action-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="16">
          <el-button-group>
            <el-button
                type="success"
                plain
                icon="Edit"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['system:sale:edit']"
                size="medium"
            >修改</el-button>
            <el-button
                type="danger"
                plain
                icon="Delete"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['system:sale:remove']"
                size="medium"
            >删除</el-button>
            <el-button
                type="warning"
                plain
                icon="Download"
                @click="handleExport"
                v-hasPermi="['system:sale:export']"
                size="medium"
            >导出</el-button>
          </el-button-group>
        </el-col>
        <el-col :span="8" class="text-right">
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-col>
      </el-row>
    </div>

    <!-- 数据表格 -->
    <div class="table-card">
      <el-table
          v-loading="loading"
          :data="saleList"
          @selection-change="handleSelectionChange"
          stripe
          height="calc(100vh - 380px)"
          style="width: 100%"
      >
        <el-table-column type="selection" width="48" align="center" fixed="left" />
        <el-table-column label="售后ID" align="center" prop="afterSaleId" width="100" fixed="left" />
        <el-table-column label="订单ID" align="center" prop="orderId" width="100" />
        <el-table-column label="售后类型" align="center" prop="type" width="120">
          <template #default="scope">
            <dict-tag :options="mall_after_sale_type" :value="scope.row.type"/>
          </template>
        </el-table-column>
        <el-table-column label="售后原因" align="center" prop="reason" min-width="150" show-overflow-tooltip />
        <el-table-column label="申请金额" align="center" prop="amount" width="120">
          <template #default="scope">
            <span class="amount-text">¥{{ scope.row.amount }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status" width="120">
          <template #default="scope">
            <dict-tag :options="mall_after_sale_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="管理员备注" align="center" prop="adminRemark" min-width="150" show-overflow-tooltip />
        <el-table-column label="创建时间" align="center" prop="createTime" width="160">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新时间" align="center" prop="updateTime" width="160">
          <template #default="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120" fixed="right">
          <template #default="scope">
            <div class="action-buttons">
              <el-button
                  link
                  type="primary"
                  icon="Edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['system:sale:edit']"
                  size="small"
              >修改</el-button>
              <el-button
                  link
                  type="danger"
                  icon="Delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['system:sale:remove']"
                  size="small"
              >删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="pagination-card">
      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </div>

    <!-- 添加或修改对话框 -->
    <el-dialog
        :title="title"
        v-model="open"
        width="600px"
        append-to-body
        class="form-dialog"
    >
      <el-form ref="saleRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="售后类型" prop="type">
              <el-select
                  v-model="form.type"
                  placeholder="请选择售后类型"
                  style="width: 100%"
              >
                <el-option
                    v-for="dict in mall_after_sale_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请金额" prop="amount">
              <el-input
                  v-model="form.amount"
                  placeholder="请输入申请金额"
                  type="number"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="售后原因" prop="reason">
          <el-input
              v-model="form.reason"
              type="textarea"
              placeholder="请输入售后原因"
              :rows="3"
          />
        </el-form-item>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="售后状态" prop="status">
              <el-select
                  v-model="form.status"
                  placeholder="请选择状态"
                  style="width: 100%"
              >
                <el-option
                    v-for="dict in mall_after_sale_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="管理员备注" prop="adminRemark">
          <el-input
              v-model="form.adminRemark"
              type="textarea"
              placeholder="请输入管理员备注"
              :rows="2"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Sale">
import { listSale, getSale, delSale, addSale, updateSale } from "@/api/system/sale"

const { proxy } = getCurrentInstance()
const { mall_after_sale_type, mall_after_sale_status } = proxy.useDict('mall_after_sale_type', 'mall_after_sale_status')

const saleList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    afterSaleId: null,
    orderId: null,
    userId: null,
    type: null,
    reason: null,
    amount: null,
    status: null,
    images: null,
    adminRemark: null,
    createTime: null,
    updateTime: null
  },
  rules: {
    orderId: [
      { required: true, message: "订单ID不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "用户ID不能为空", trigger: "blur" }
    ],
    type: [
      { required: true, message: "请选择售后类型", trigger: "change" }
    ],
    reason: [
      { required: true, message: "售后原因不能为空", trigger: "blur" }
    ],
    amount: [
      { required: true, message: "申请金额不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "请选择售后状态", trigger: "change" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询售后管理列表 */
function getList() {
  loading.value = true
  listSale(queryParams.value).then(response => {
    saleList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    afterSaleId: null,
    orderId: null,
    userId: null,
    type: null,
    reason: null,
    amount: null,
    status: null,
    images: null,
    adminRemark: null,
    createTime: null,
    updateTime: null
  }
  proxy.resetForm("saleRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.afterSaleId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _afterSaleId = row.afterSaleId || ids.value
  getSale(_afterSaleId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改售后管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["saleRef"].validate(valid => {
    if (valid) {
      if (form.value.afterSaleId != null) {
        updateSale(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addSale(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _afterSaleIds = row.afterSaleId || ids.value
  proxy.$modal.confirm('是否确认删除售后管理编号为"' + _afterSaleIds + '"的数据项？').then(function() {
    return delSale(_afterSaleIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/sale/export', {
    ...queryParams.value
  }, `sale_${new Date().getTime()}.xlsx`)
}

getList()
</script>

<style scoped>
.page-header {
  margin-bottom: 20px;
  padding: 0 20px;
}

.page-title {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 8px;
}

.page-subtitle {
  font-size: 14px;
  color: #909399;
}

.search-card {
  background: #fff;
  padding: 20px;
  border-radius: 4px;
  margin-bottom: 16px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

.search-row {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.search-row:last-child {
  margin-bottom: 0;
}

.search-row .el-form-item {
  margin-right: 16px;
  margin-bottom: 0;
}

.search-actions {
  margin-left: auto;
}

.action-card {
  background: #fff;
  padding: 16px 20px;
  border-radius: 4px;
  margin-bottom: 16px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

.table-card {
  background: #fff;
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

.pagination-card {
  background: #fff;
  padding: 16px 20px;
  border-radius: 4px;
  margin-top: 16px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

.amount-text {
  font-weight: 600;
  color: #f56c6c;
}

.action-buttons {
  display: flex;
  gap: 8px;
}

.text-right {
  text-align: right;
}

.form-dialog .el-form-item {
  margin-bottom: 20px;
}

:deep(.el-table .cell) {
  padding: 8px 12px;
}

:deep(.el-table th) {
  background-color: #f5f7fa;
  color: #606266;
  font-weight: 600;
}
</style>